package com.tm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tm.ResultEntity;
import com.tm.model.SpSkuInfo;
import com.tm.service.SpSkuInfoService;
import com.wordnik.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/sku")
public class SpSkuInfoController {

	private Logger logger = Logger.getLogger(SpSkuInfoController.class);
	
	@Autowired
	private SpSkuInfoService ssiService;
	
	/**
	 * 根据分类id获取商品列表
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="根据分类id获取商品列表",notes="根据分类id获取商品列表接口")
    @RequestMapping(value="/list", method=RequestMethod.POST)
    public ResultEntity list(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------根据分类id获取商品列表接口begin-----------------------------------------");
		try {
			
			Integer typeId = Integer.parseInt(request.getParameter("typeId"));//分类id
			Integer startIndex = Integer.parseInt(request.getParameter("startIndex"));//开始下标
			Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页显示数量
			String orderType = request.getParameter("orderType");//排序方式 1：综合 2：销量 3：价格由低到高 4：价格由高到低
			
			re = ssiService.getSkuListByTypeId(typeId, startIndex, pageSize, orderType);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------根据分类id获取商品列表接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 根据关键字获取商品列表
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="根据关键字获取商品列表",notes="根据关键字获取商品列表接口")
    @RequestMapping(value="/listByKeyword", method=RequestMethod.POST)
    public ResultEntity listByKeyword(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------根据关键字获取商品列表接口begin-----------------------------------------");
		try {
			
			String keyword = request.getParameter("keyword");//关键字
			Integer startIndex = Integer.parseInt(request.getParameter("startIndex"));//开始下标
			Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页显示数量
			String orderType = request.getParameter("orderType");//排序方式 1：综合 2：销量 3：价格由低到高 4：价格由高到低
			
			re = ssiService.getSkuListByKeyword(keyword, startIndex, pageSize, orderType);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------根据关键字获取商品列表接口end-----------------------------------------");
		
        return re;
    }
	
	
	/**
	 * 收藏商品（取消收藏）
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="收藏商品",notes="收藏商品接口")
    @RequestMapping(value="/collect", method=RequestMethod.POST)
    public ResultEntity collect(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------收藏商品接口begin-----------------------------------------");
		try {
			
			Integer skuId = Integer.parseInt(request.getParameter("skuId"));//商品id
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			re = ssiService.addSpSkuCollect(skuId, memberId);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------收藏商品接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 删除购物车明细
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="删除购物车明细",notes="删除购物车明细接口")
    @RequestMapping(value="/delShopCartDetail", method=RequestMethod.POST)
    public ResultEntity delShopCartDetail(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------删除购物车明细接口begin-----------------------------------------");
		try {
			
			String ids = request.getParameter("ids");
			
			re = ssiService.delShopCartDetail(ids);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------删除购物车明细接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 修改购物车明细
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="修改购物车明细",notes="修改购物车明细接口")
    @RequestMapping(value="/updateShopCartDetail", method=RequestMethod.POST)
    public ResultEntity updateShopCartDetail(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------修改购物车明细接口begin-----------------------------------------");
		try {
			
			Integer id = Integer.parseInt(request.getParameter("id"));
			Integer cnt = Integer.parseInt(request.getParameter("cnt"));
			
			re = ssiService.updateShopCartDetail(id, cnt);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------修改购物车明细接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 加入购物车
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="加入购物车",notes="加入购物车接口")
    @RequestMapping(value="/addShopCart", method=RequestMethod.POST)
    public ResultEntity addShopCart(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------加入购物车接口begin-----------------------------------------");
		try {
			
			boolean flag = (boolean) request.getSession().getAttribute("isBind");
			
			if(!flag){
				re = new ResultEntity("-2", "请先绑定手机！");
				return re;
			}
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			Integer skuId = Integer.parseInt(request.getParameter("skuId"));
			String skuNo = request.getParameter("skuNo");
			String skuUnit = request.getParameter("skuUnit");
			String skuPrice = request.getParameter("skuPrice");
			String skuAmount = request.getParameter("skuAmount");
			
			//获取商品基本信息
			SpSkuInfo sku = ssiService.getSpSkuInfoBySkuId(skuId);
			
			if(StringUtils.isEmpty(skuAmount)){
				if(sku.getStockAmountRest() == 0){
					re = new ResultEntity("-1", "库存不足！");
					return re;
				}
				skuNo = sku.getSkuNo();
				skuUnit = sku.getSkuUnit();
				skuPrice = sku.getPromotionPrice().toString();
				skuAmount = "1";
			}else{
				if(sku.getStockAmountRest() < Integer.parseInt(skuAmount)){
					re = new ResultEntity("-1", "库存不足！");
					return re;
				}
			}
			
			re = ssiService.addShopCart(skuId, memberId, skuNo, skuUnit, skuPrice, skuAmount);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------加入购物车接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 拼团
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="拼团",notes="拼团接口")
    @RequestMapping(value="/joinGroup", method=RequestMethod.POST)
    public ResultEntity joinGroup(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------拼团接口begin-----------------------------------------");
		try {
			
			boolean flag = (boolean) request.getSession().getAttribute("isBind");
			
			if(!flag){
				re = new ResultEntity("-2", "请先绑定手机！");
				return re;
			}
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			Integer skuId = Integer.parseInt(request.getParameter("skuId"));
			Integer spellId = Integer.parseInt(request.getParameter("spellId"));
			String money = request.getParameter("money");
			
			re = ssiService.addSpSpellGroupParticipate(skuId, memberId, money, spellId);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------拼团接口end-----------------------------------------");
		
        return re;
    }
	
	/**
	 * 获取会员收藏商品列表
	 * @param mobile
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="获取会员收藏商品列表",notes="获取会员收藏商品列表接口")
    @RequestMapping(value="/collectList", method=RequestMethod.POST)
    public ResultEntity collectList(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------获取会员收藏商品列表接口begin-----------------------------------------");
		try {
			
			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			Integer startIndex = Integer.parseInt(request.getParameter("startIndex"));//开始下标
			Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页显示数量
			
			re = ssiService.getCollectListByMemberId(memberId, startIndex, pageSize);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------获取会员收藏商品列表接口end-----------------------------------------");
		
        return re;
    }
	
	
}
